<?php

namespace App\Http\Controllers\admin\user;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\User_info;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $where = [];
        $user = new User();
        $bo = $user->sel();
        if($request->has('username')){
            $username = $request->input('username');
            $bo->where('username','like',"%{$username}%");
            $where['username'] = $username;
        }
        $list = $bo->get();
        return view('admin.user.userlist', ['list' => $list, 'where'=>$where]);
    }

    public function create()
    {
        return view('admin.user.adduser');
    }



    public function edit($id)
    {
        $user = new User();
        $ob = $user->sel()->where('user.id',$id)->first();
        return view('admin.user.updateuser',['list' => $ob]);
    }
    public function update($id,Request $request)
    {
        $username = $request->input('username');
        $password = $request->input('password');
        if($username != ''){
            $user  = new User();
            $bn = $user->adminuser($username , $id);
            if(!$bn){
                return redirect()->back()->with('msg',1);
            }
        }
        if($password != ''){
            $user  = new User();
            $bn = $user->adminpwd($password , $id);
            if(!$bn){
                return redirect()->back()->with('msg',2);
            }
        }
        $user_info = new User_info();
        $row = $user_info->userUpdate($request, $id);
        if($row>0){
            return redirect('/admin/user/userlist/'.$id.'/edit')->with('msg',3);
        }else{
            return redirect()->back();
        }
    }

}
